package com.uplift.mgt.user.controller;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.uplift.common.Const;
import com.uplift.common.result.IResultable;
import com.uplift.common.result.ResultWrapper4Layui;
import com.uplift.mgt.user.model.User;
import com.uplift.mgt.user.service.IUserService;

/**
 * 用户信息相关
 * 
 * @author bcwang
 *
 */
@Controller
@RequestMapping(value = "/user")
public class UserController {

	@Autowired
	private IUserService userService;

	/**
	 * 用户登录
	 * 
	 * @param request
	 * @param user
	 * @return IResultable
	 */
	@RequestMapping(value = "/login")
	@ResponseBody
	public IResultable login(HttpServletRequest request, User user) {
		// 登录状态
		int ret = this.userService.login(request, user);
		// 根据状态返回登录信息
		if (ret == 1) {
			Map<?, ?> userMap = (Map<?, ?>) request.getSession().getAttribute(Const.SESSION_USER);
			userMap.remove("status");
			userMap.remove("password");
			userMap.remove("createTime");
			return ResultWrapper4Layui.wrapMap(userMap, ResultWrapper4Layui.SUCC, "登录成功");
		} else if (ret == 2) {
			return ResultWrapper4Layui.wrapMap(null, ResultWrapper4Layui.FAIL, "登录失败，账号和密码不能为空");
		} else if (ret == 3) {
			return ResultWrapper4Layui.wrapMap(null, ResultWrapper4Layui.FAIL, "登录失败，账号被禁用");
		}  else {
			return ResultWrapper4Layui.wrapMap(null, ResultWrapper4Layui.FAIL, "登录失败，账号或密码错误");
		}
	}

	/**
	 * 登出
	 * 
	 * @param request
	 * @param user
	 * @return IResultable
	 */
	@RequestMapping(value = "/logout")
	@ResponseBody
	public IResultable logout(HttpServletRequest request, User user) {
		try {
			HttpSession session = request.getSession();
			session.invalidate();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return ResultWrapper4Layui.wrapMap(null, ResultWrapper4Layui.SUCC, "注销成功");
	}

	/**
	 * 用户列表
	 * 
	 * @param user
	 * @return IResultable
	 */
	@RequestMapping(value = "/list")
	@ResponseBody
	public IResultable list(User user) {
		try {
			List<Map<String, Object>> list = this.userService.queryUser(user);
			return ResultWrapper4Layui.wrapListPage(list, ResultWrapper4Layui.SUCC, "查询成功");
		} catch (Exception e) {
			return ResultWrapper4Layui.wrapListPage(null, ResultWrapper4Layui.FAIL, "查询失败");
		}

	}

	/**
	 * 新增用户
	 * 
	 * @param request
	 * @param info
	 * @return IResultable
	 */
	@RequestMapping(value = "/save")
	@ResponseBody
	public IResultable save(User user) {
		try {
			int status = this.userService.saveUser(user);
			if (status == 1) {
				return ResultWrapper4Layui.wrapMap(null, ResultWrapper4Layui.SUCC, "保存成功");
			} else if (status == 2){
				return ResultWrapper4Layui.wrapMap(null, ResultWrapper4Layui.FAIL, "保存失败，重复的账号名称");
			} else {
				return ResultWrapper4Layui.wrapMap(null, ResultWrapper4Layui.FAIL, "保存失败");
			}
		} catch (Exception e) {
			return ResultWrapper4Layui.wrapMap(null, ResultWrapper4Layui.FAIL, "保存失败");
		}
	}

	/**
	 * 修改用户
	 * 
	 * @param info
	 * @return IResultable
	 */
	@RequestMapping(value = "/modify")
	@ResponseBody
	public IResultable modify(User user) {
		try {
			int row = this.userService.modifyUser(user);
			if (row > 0) {
				return ResultWrapper4Layui.wrapMap(null, ResultWrapper4Layui.SUCC, "修改成功");
			} else {
				return ResultWrapper4Layui.wrapMap(null, ResultWrapper4Layui.FAIL, "修改失败");
			}
		} catch (Exception e) {
			return ResultWrapper4Layui.wrapMap(null, ResultWrapper4Layui.FAIL, "修改失败");
		}
	}

	/**
	 * 删除用户
	 * 
	 * @param user
	 * @return IResultable
	 */
	@RequestMapping(value = "/remove")
	@ResponseBody
	public IResultable remove(User user) {
		try {
			int row = this.userService.removeUser(user);
			if (row > 0) {
				return ResultWrapper4Layui.wrapMap(null, ResultWrapper4Layui.SUCC, "删除成功");
			} else {
				return ResultWrapper4Layui.wrapMap(null, ResultWrapper4Layui.FAIL, "删除失败");
			}
		} catch (Exception e) {
			return ResultWrapper4Layui.wrapMap(null, ResultWrapper4Layui.FAIL, "删除失败");
		}
	}
}
